package com.fage.spring.impl;

import com.fage.pojo.User;
import com.fage.spring.UserDao;
import com.fage.spring.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

/**
 * @author lin
 * @version 1.0
 * @date 2020-08-10 11:17
 * @Description TODO
 */
@Service(value = "userService")
public class UserServiceImpl implements UserService {
    @Autowired
    @Qualifier("userDao")
    private UserDao userDao;

    @Override
    public User saveUser(User user) {
        return userDao.saveUser(user);
    }

    @Override
    public void transfer(String sourceName, String targetName, Float money) {
        //根据名称查询两个账户信息
        User source = userDao.findByName(sourceName);
        User target = userDao.findByName(targetName);
        //转出账户减钱，转入账户加钱
        source.setMoney(source.getMoney() - money);
        target.setMoney(target.getMoney() + money);
        //更新两个账户
        userDao.update(source);
        //模拟转账异常
        int i = 1 / 0;
        userDao.update(target);
    }
}
